<?php

namespace App\Evaluation\Controllers;


use App\Evaluation\Actions\Post\PatentAssessPost;
use App\Evaluation\Actions\Post\PatentAssessReturnPost;
use App\Http\Controllers\Controller;
use App\PatentAssess;
use App\Repositories\PatentApplyRepository;
use App\Services\SettingService;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;

class PatentAssessController extends Controller
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '专利申请前评估';
    public $repository;

    public function __construct(PatentApplyRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index(Content $content): Content
    {
        $grid = $this->grid();
        $grid->model()->where('state', 0);
        return $content->title('待评估')->body($grid);
    }

    public function ypg(Content $content): Content
    {
        $grid = $this->grid();
        $grid->model()->where('state', 1);
        return $content->title('已评估专利')->body($grid);
    }

    protected function grid(): Grid
    {
        $user = Admin::user();
        $grid = new Grid(new PatentAssess());
        $grid->model()->has('apply')->with(['apply.department']);
        $grid->model()->where('expert_id', $user['id']);
        $grid->column('apply.patent_name', __('专利名称'));
        $grid->column('apply.charge_person', __('第一发明人'));
        $grid->column('apply.department', __('dept_name'))->display(function ($value) {
            return $value['name'] ?? '';
        });
        $grid->column('remark', '评估意见')->display(function ($value) {
            return $value ? '查看' : '';
        })->modal('评估意见', function ($model) {
            return $model->remark;
        });
        $grid->column('score', '评分')->display(function ($score) {
            return $score > 0 ? $score : '';
        })->sortable();
        $grid->column('path', '评估报告')->image()->display(function ($path) {
            return $path ? '查看' : '';
        })->link(function (PatentAssess $model) {
            $apply = $model->apply;
            return file_custom_preview_url($model['path'], '(评估报告)' . $apply->patent_name, 'show');
        });
        $grid->column('score_template', '评分指标')->display(function () {
            return SettingService::patentPfTemplateATag();
        });
        $grid->column('table', '评估表')->display(function () {
            return '下载';
        })->link(function (PatentAssess $assess) {
            $model = $assess->apply;
            return admin_route('files.patent-apply.table', [$model['id'], 'token' => $model->token()]);
        });
        $grid->column('apply.file', '交底书')->display(function ($value) {
            return $value ? '下载' : '';
        })->link(function (PatentAssess $model) {
            $apply = $model->apply;
            return file_custom_preview_url($apply['file'], '(交底书)' . $apply->patent_name);
        });
        $grid->batchActions(function (Grid\Tools\BatchActions $actions) {
            $actions->disableDeleteAndHodeSelectAll();
        });
        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableView();
            $actions->disableDelete();
            $actions->disableEdit();
            $model = $actions->row;
            if (!$model->state || request()->get('admin') == 'jwb123') {
                $actions->append(new PatentAssessPost($actions));
                $actions->append(new PatentAssessReturnPost($actions));
            }
        });
        $grid->filter(function (Grid\Filter $filter) {
            $filter->disableIdFilter();
        });
        return $grid;
    }

}
